using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

[Serializable]
public class ZombieBlackBoard : BlackBoard
{
    public float idleTime;
    public float moveSpeed;
    public Transform transform;
}
public class AI_Idle : Istate
{
    private float idleTimer;
    private FSM fsm;
    private ZombieBlackBoard blackBoard;

    public AI_Idle(FSM fsm)
    {
        this.fsm = fsm;
        this.blackBoard = fsm.blackBoard as ZombieBlackBoard;
    }
    public void OnEnter()
    {
        idleTimer = 0;
    }

    public void OnExit()
    { 
    }

    public void OnUpdate()
    {
        idleTimer += Time.deltaTime;
        if (idleTimer>blackBoard.idleTime)
        {
            this.fsm.SwitchState(StateType.Move);
        }
    }
}
public class AI_MoVe : Istate
{
    private FSM fsm;
    private ZombieBlackBoard blackBoard;
    private Vector3 targetPos;
    public AI_MoVe(FSM fsm)
    {
        this.fsm = fsm;
        this.blackBoard = fsm.blackBoard as ZombieBlackBoard;
    }
    public void OnEnter()
    {
        float randomX =UnityEngine.Random.Range(-10, 10);
        float randomZ = UnityEngine.Random.Range(-10, 10);
        targetPos=new Vector3(blackBoard.transform.position.x+randomX,0, blackBoard.transform.position.z+randomZ);
    }

    public void OnExit()
    {
        
    }

    public void OnUpdate()
    {
        if (Vector3.Distance(blackBoard.transform.position, targetPos) <= 0.1f)
        {
            fsm.SwitchState(StateType.Idle);
        }
        else
        {
            blackBoard.transform.LookAt(targetPos);
            blackBoard.transform.Translate(Vector3.forward*Time.deltaTime*blackBoard.moveSpeed);
        }
        
    }
}

public class Monster : MonoBehaviour
{
    private FSM fsm;
    public ZombieBlackBoard blackBoard;
    // Start is called before the first frame update
    void Start()
    {
        fsm = new FSM(blackBoard);
        fsm.AddState(StateType.Idle,new AI_Idle(fsm));
        fsm.AddState(StateType.Move,new AI_MoVe(fsm));
        fsm.SwitchState(StateType.Idle);
    }

    // Update is called once per frame
    void Update()
    {
       
        fsm.OnUpdate();
    }
    private void FixedUpdate()
    {
        
    }
}
